extends Node

var utils = null
var ctrl = null
var base = null
var path = null
var window
var context
var file = File.new()


func _ready():
	pass
func _init():
	call_deferred("init")
func init():
	if globalData.infoDs.has("g_AneHekBase"):
		base = globalData.infoDs["g_AneHekBase"]
		path = chaData.infoDs["cTDW"].dir
	if base != null:
		#初始化
		utils = globalData.infoDs["g_AneHekUtils"]
		ctrl = globalData.infoDs["g_AneHek"]
		base.connect("onSaveGame",self,"resetData")
		base.connect("onStartGame", self, "createIllstrate")
#重置数据
func resetData():
	if is_instance_valid(window):
		context.queue_free()
		window.queue_free()
		context = null
		window = null
#创建角色按钮
func newChaBt(node, id):
	var chaBt = preload("res://ui/itemBt/itemBt.tscn").instance()
	chaBt.id = id
	node.add_child(chaBt)
#创建窗口
func createIllstrate():
	var baseui = sys.main.get_node("ui")
	#baseui.print_tree_pretty()
	window = utils.cWin(baseui,"塔防之光英雄图鉴",Vector2(1300,800))
	context = TabContainer.new()
	context.set_size(Vector2(1240,730))
	context.set_position(Vector2(30,30))
	window.add_child(context)
	context.tab_align = TabContainer.ALIGN_LEFT
	context.modulate = "#FFFFFF"
	context.self_modulate = "#FFFFFF"
	#context.show_behind_parent = true
	var names = ["法师", "战士", "射手", "牧师", "刺客", "骷髅","软泥怪","娜迦","蟹族","召唤物"]
	for i in range(names.size()):
		var tab = Tabs.new()
		tab.name = names[i]
		context.add_child(tab)
		var tmp = utils.cSc(tab,Vector2(1200,680),Vector2(10,10))
		#tab.add_child(tmp)
		var taskBox = HBoxContainer.new()
		tmp.add_child(taskBox)
		addlist(names[i],taskBox)

func addlist(keyword:String,taskBox):
	#print(keyword)
	var id = ""
	match keyword:
		"法师":
			id = "cTDM"
		"战士":
			id = "cTDW"
		"牧师":
			id = "cTDP"
		"刺客":
			id = "cTDA"
		"骷髅":
			id = "cTDZ_2"
		"软泥怪":
			id = "cTDZ_1"
		"射手":
			id = "cTDR"
		"娜迦":
			id = "cTDZ_3"
		"蟹族":
			id = "cTDZ_4"
		#print(id)

			
	var vbox = VBoxContainer.new()
	taskBox.add_child(vbox)
	var hBox1 = HBoxContainer.new()
	hBox1.alignment = 1
	vbox.add_child(hBox1)
	#utils.cBtns(vbox,self,"兑换","exchange",[id])
	if keyword == "召唤物":
		for i in utils.st:
			newChaBt(hBox1,i)
		return
	newChaBt(hBox1,id)
	hBox1.set_custom_minimum_size(Vector2(200, 100))
	var hBox24 = HBoxContainer.new()
	hBox24.alignment = 1
	vbox.add_child(hBox24)
	#2-4分容器
	var vBox3 = VBoxContainer.new()
	vBox3.alignment = 1
	hBox24.add_child(vBox3)
	#2级单位容器
	var hBox2 = HBoxContainer.new()
	hBox2.alignment = 1
	hBox2.set_custom_minimum_size(Vector2(200, 150))
	vBox3.add_child(hBox2)
	#3级单位总容器
	var hBox33 = HBoxContainer.new()
	hBox33.alignment = 1
	vBox3.add_child(hBox33)
	#3-4级单位分容器
	var vBox34 = VBoxContainer.new()
	vBox34.alignment = 1
	hBox33.add_child(vBox34)
	#3级单位容器
	var hBox3 = HBoxContainer.new()
	hBox3.alignment = 1
	hBox3.set_custom_minimum_size(Vector2(200, 150))
	vBox34.add_child(hBox3)
	var hBox4 = HBoxContainer.new()
	hBox4.alignment = 1
	hBox4.set_custom_minimum_size(Vector2(200, 150))
	vBox34.add_child(hBox4)
	var tmp = 1
	var file = File.new()
	while(file.file_exists(path + "/" + id + "_" + str(tmp) + "/" + id + "_" + str(tmp) + ".gd")):
		var nowid = id + "_" + str(tmp)
		newChaBt(hBox2,nowid)
		newChaBt(hBox3,nowid + "_1")
		if id.find("cTDZ") == -1:
			newChaBt(hBox4,nowid + "_1_1")
		else:
			var exid = nowid + "_1"
			exid = exid.replace("_","").replace("cTDZ","cex___TDZ")
			newChaBt(hBox4,exid)
		tmp += 1

#弹出窗口
func openit():
	window.popup_centered()